<?php

namespace App\Http\Controllers\Admin;


use App\Admin\Manager;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class MemberController extends Controller
{
    //
    public function list(){
        $data = Manager::where('sta','<>',2)->paginate(10);
        return view('admin.member.list',compact('data'));
    }
    public function stop(Request $request){
        $data =  $request->route( 'id' );
        Manager::where('id',$data)->update(['sta'=>1]);
        return json_encode(['message'=>'禁用成功']);
    }
    public function start(Request $request){
        $data =  $request->route( 'id' );
        Manager::where('id',$data)->update(['sta'=>0]);
        return json_encode(['message'=>'启用成功']);
    }
    public function del(Request $request){
        $data =  $request->route( 'id' );
        Manager::where('id',$data)->update(['sta'=>2]);
        return json_encode(['message'=>'删除成功']);
    }
    public function edit(Request $request){
        $data =  $request->route( 'id' );
        $data =  Manager::where('id',$data)->get();
        return view('admin/member/edit',compact('data'));
    }
    public function upload(Request $request ){
        // 设置超时时间为没有限制
        ini_set("max_execution_time", "0");

        $http_type = ((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $file = $request->file('file');

        $allowed_extensions = ["png", "jpg", "gif", "jpeg", "bmp"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return json_encode(['error' => 'You may only upload png, jpg or gif or jpeg or bmp.']);
        }

        $destinationPath = 'storage/uploads/'.date('Ymd').'/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = md5(microtime(true)).'.'.$extension;
        $file->move($destinationPath, $fileName);
        Cookie::queue('user_img', $destinationPath.$fileName, 5);
    }

    public function edit_up( Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            $user_img = Cookie::get('user_img');

            if (!empty($user_img)){

                DB::table('manager')->where('id','=',$data['id'])
                    ->update(
                        [
                            'email'=>$data['email'],
                            'name'=>$data['name'],
                            'sex'=>$data['sex'],
                            'password'=> bcrypt($data['password']),
                            'user_img'=>Cookie::get('user_img'),
                        ]
                    );
                Cookie::queue('user_img',null,-1);
                return json_encode(['message'=>'修改成功1']);
            }else{

                DB::table('manager')->where('id','=',$data['id'])
                    ->update(
                        [
                            'email'=>$data['email'],
                            'name'=>$data['name'],
                            'sex'=>$data['sex'],
                            'password'=> bcrypt($data['password']),
                        ]
                    );
                return json_encode(['message'=>'修改成功']);
            }
        }

    }
    public function add(){
        return view('admin/member/add');
    }
    public function add_up(Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            unset($data['_token']);
            $data['user_img'] = Cookie::get('user_img');
            $data['password'] = bcrypt($data['password']);
            $data['created_at'] = date('Y-m-d H:i:s');
            DB::table('manager')->insert($data);
            Cookie::queue('user_img',null,-1);
            return json_encode(['message'=>'添加成功']);
        }
    }
    public function all_del(Request $request){
        $data =  $request->all();
        foreach ($data['id'] as $val){
//            echo $val.'<br>';
            DB::table('manager')->where('id',$val)->update(['sta'=>2]);

        }
        return json_encode(['message'=>'删除成功']);
    }

}
